﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using boe2.irc;
using System.Windows.Media.Effects;

namespace wpfirc
{
    /// <summary>
    /// Interaction logic for IrcEntry.xaml
    /// </summary>
    public partial class IrcEntry : UserControl
    {
        public Boolean ShowTimeStamp { get; set; }

		public override string ToString()
		{
			if (IrcMessage != null)
			{
				return String.Format("{0}| <{1}> {2}", IrcMessage.TimeStamp.ToShortTimeString(), IrcMessage.Prefix.Nick ?? "", IrcMessage.Message);
			}
			return "";
		}

		public Boolean IsSelected
		{
			get { return (Boolean)GetValue(IsSelectedProperty); }
			set { SetValue(IsSelectedProperty, value); }
		}
		public static readonly DependencyProperty IsSelectedProperty =
			DependencyProperty.Register("IsSelected", typeof(Boolean), typeof(IrcEntry), new UIPropertyMetadata(new PropertyChangedCallback(SelectedChanged)));

		public IrcMessage IrcMessage
		{
			get { return (IrcMessage)GetValue(IrcMessageProperty); }
			set { SetValue(IrcMessageProperty, value); }
		}
		public static readonly DependencyProperty IrcMessageProperty =
			DependencyProperty.Register("IrcMessage", typeof(IrcMessage), typeof(IrcEntry), new FrameworkPropertyMetadata(null));

		public IrcEntry()
		{
			InitializeComponent();
			ShowTimeStamp = true;
			IsSelected = false;
		}
		
		private static void SelectedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			IrcEntry entry = (IrcEntry)d;
			if ((Boolean)e.NewValue)
			{
				entry.Background = new SolidColorBrush(Color.FromRgb(200, 200, 200));
			}
			else
			{
				entry.Background = null;
			}
		}

		private void TimeStampField_MouseEnter(object sender, MouseEventArgs e)
		{
			this.BorderBrush = new SolidColorBrush(Color.FromRgb(0, 0, 0));
			this.BorderThickness = new Thickness(1);
			this.Margin = new Thickness(-1);
		}

		private void TimeStampField_MouseLeave(object sender, MouseEventArgs e)
		{
			this.BorderBrush = null;
			this.BorderThickness = new Thickness(0);
			this.Margin = new Thickness(0);
		}

		private void TimeStampField_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
		{
			IsSelected = !IsSelected;
		}
    }
}
